package cn.xz.zysjz.activity;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;

import cn.xz.zysjz.R;
import cn.xz.zysjz.adapter.ActivityListAdapter;
import cn.xz.zysjz.base.BaseActivity;
import cn.xz.zysjz.bean.ActivityListBean;
import cn.xz.zysjz.bean.AppData;
import cn.xz.zysjz.bean.HomeListBean;
import cn.xz.zysjz.databinding.ActivitySouBinding;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 搜索*/
public class SouActivity extends BaseActivity {

    ActivitySouBinding mBinding;

    private ActivityListAdapter mAdapter; //列表适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_sou);
        initEvent();
        //列表设置
        initRV();
    }

    private void initRV(){
        //列表相关数据
        LinearLayoutManager ms = new LinearLayoutManager(this);
        mAdapter = new ActivityListAdapter(R.layout.item_activity_list);
        mBinding.rvList.setLayoutManager(ms);
        mBinding.rvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ArrayList<HomeListBean> data = (ArrayList<HomeListBean>) adapter.getData();
                //跳转到详情页
                AppHelper.toJump(IndexActivity.class,data.get(position));
            }
        });
    }

    private void initEvent(){
        //返回
        mBinding.ivBack.setOnClickListener(v->{
            finish();
        });
        //搜索
        mBinding.tvS.setOnClickListener(v->{
            String mS = mBinding.etS.getText().toString();
            if (mS.isEmpty()){
                ToastUtils.show("请输入内容");
                return;
            }
            ArrayList<HomeListBean> ml = new ArrayList<>();
            ArrayList<HomeListBean> show = new ArrayList<>();
            ArrayList<HomeListBean> ml1 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVac,HomeListBean.class);
            ArrayList<HomeListBean> ml2 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVFood,HomeListBean.class);
            ArrayList<HomeListBean> ml3 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVSlp,HomeListBean.class);
            ArrayList<HomeListBean> ml4 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVMin,HomeListBean.class);
            ml.addAll(ml1);
            ml.addAll(ml2);
            ml.addAll(ml3);
            ml.addAll(ml4);
            for (HomeListBean homeListBean: ml){
                if (homeListBean.getMsg().contains(mS)){
                    show.add(homeListBean);
                }
            }
            mAdapter.setList(show);
            if (show.size() == 0){
                ToastUtils.show("未搜索到相关内容");
            }
        });
    }
}